"""
@FileName：template_manage.py\n
@Description：\n
@Author：DengHong\n
@Time：2024/7/12 10:43\n
@Department：技术支持部\n

"""
import time
import pyautogui
from selenium.webdriver import Keys
from common.logger import GetLogger
import get_project_path
from common.file_load import *

class template_manage:
    def __init__(self, driver):
        self.driver = driver
        self.logger = GetLogger.get_logger()
        self.page_eles = load_yaml_file('/pagefiles/strategy_admin.yaml')["StrategyTemplatePage"]
    def add_template_group(self,group_name):
        ele_info = self.page_eles["add_template_group"]
        self.driver.click(ele_info=ele_info["新增模板组"])
        time.sleep(1)
        self.driver.send_keys(ele_info=ele_info["输入模板描述"], text=group_name)
        self.driver.send_keys(ele_info=ele_info["输入模板组名称"], text=group_name)
        self.driver.click(ele_info=ele_info["下一步"])
    def enter_MouldChildList(self, group_name):
        ele_info = self.page_eles["template_list"]
        current_url = self.driver.get_url()
        path = current_url.split("/")[-1]
        if 'MouldChildList' in path:
            self.logger.error(f'已进入模板组')
            return
        time.sleep(1)
        self.driver.context_click(ele_info=ele_info["模板管理"])
        time.sleep(1)
        self.driver.click(ele_info=ele_info["关闭其他"])
        time.sleep(1)
        self.driver.send_keys(ele_info=ele_info["输入模板组名称条件"], text=f"{Keys.CONTROL}, 'a'")
        self.driver.action_keys(Keys.BACKSPACE)
        time.sleep(1)
        self.driver.send_keys(ele_info=ele_info["输入模板组名称条件"], text=group_name)
        self.driver.click(ele_info=ele_info["模板组查询"])
        time.sleep(1)
        self.driver.click(ele_info=ele_info["模板组配置"])

    def query_template(self,template_name):
        ele_info = self.page_eles["template_list"]
        self.driver.send_keys(ele_info=ele_info["输入模板名称条件"], text=f"{Keys.CONTROL}, 'a'")
        time.sleep(1)
        self.driver.action_keys(Keys.BACKSPACE)
        time.sleep(1)
        self.driver.send_keys(ele_info=ele_info["输入模板名称条件"], text=template_name)
        self.driver.click(ele_info=ele_info["模板查询"])
        time.sleep(1)

    def add_template(self,type, group_name,template_name):
        ele_info = self.page_eles["add_template"]
        self.enter_MouldChildList(group_name)
        time.sleep(1)
        self.driver.click(ele_info=ele_info["新增"])
        self.driver.click(ele_info=ele_info["选择模板类型"])
        self.driver.click(ele_info=ele_info[type])
        self.driver.click(ele_info=ele_info["选择发送通道"])
        self.driver.click(ele_info=ele_info["选择通道列表第一项"])
        self.driver.send_keys(ele_info=ele_info["输入模板名称"], text=template_name)
        self.driver.click(ele_info=ele_info["确定"])

    def add_gzh_template(self,group_name,template_name):
        ele_info = self.page_eles["add_template"]
        self.enter_MouldChildList(group_name)
        time.sleep(1)
        self.driver.click(ele_info=ele_info["新增"])
        self.driver.click(ele_info=ele_info["选择模板类型"])
        self.driver.click(ele_info=ele_info["选择公众号模版消息"])
        self.driver.click(ele_info=ele_info["选择发送通道"])
        self.driver.click(ele_info=ele_info["选择通道列表第一项"])
        self.driver.send_keys(ele_info=ele_info["输入模板名称"], text=template_name)
        self.driver.click(ele_info=ele_info["确定"])
    def open_template(self, group_name, template_name):
        self.enter_MouldChildList(group_name)
        time.sleep(1)
        self.query_template(template_name)
        ele_info = self.page_eles["template_list"]
        time.sleep(1)
        self.driver.click(ele_info=ele_info["模板配置"])
    def edit_url_parameter(self, template_name):
        ele_info = self.page_eles["edit_sms_template"]
        self.driver.click(ele_info=ele_info["编辑url参数"])
        self.driver.send_keys(ele_info=ele_info["编辑channelSource"], text="test")
        time.sleep(1)
        self.driver.get_screenshot_as_file(rf"{get_project_path.project_path}\screenshot\解析url_{template_name}.png")
        self.driver.click(ele_info=ele_info["确定"])
        time.sleep(1)
        self.driver.click(ele_info=ele_info["点击备选"])
        self.driver.click(ele_info=ele_info["编辑url参数"])
        self.driver.send_keys(ele_info=ele_info["搜索参数"], text="保单号")
        self.driver.clicks(ele_info=ele_info["选择url参数"])
        self.driver.click(ele_info=ele_info["确定"])

    def add_parameter(self):
        ele_info = self.page_eles["edit_sms_template"]
        self.driver.click(ele_info=ele_info["+添加参数"])
        self.driver.send_keys(ele_info=ele_info["搜索参数"], text="保障额度")
        time.sleep(1)
        self.driver.action_keys(Keys.BACKSPACE)
        time.sleep(1)
        self.driver.click(ele_info=ele_info["选择参数"])
    def edit_sms_template(self,template_name):
        self.adjust_resolution()
        ele_info = self.page_eles["edit_sms_template"]
        self.driver.send_keys(ele_info=ele_info["输入发送通道模板code"],text="XMAN2023102718082")
        self.driver.click(ele_info=ele_info["选择模板用途"])
        self.driver.click(ele_info=ele_info["选择营销"])
        content = "缴费链接：{url} 保障额度："
        self.driver.send_keys(ele_info=ele_info["输入短信内容"],text=content)
        self.add_parameter()
        url = "https://www.baidu.com?platformCode=abbxsc&channelCode=DX&channelSource=xf_dxt3zx&paymentChannel=xf_dxt3zx&leadsId={leadsId}&leadsSignature={leadsSignature}"
        self.driver.send_keys(ele_info=ele_info["输入url"],text=url)
        self.edit_url_parameter(template_name)
        time.sleep(1)

    def edit_sms_5g_template(self,template_name):
        ele_info = self.page_eles["edit_sms_5g_template"]
        self.driver.click(ele_info=ele_info["开启5G消息"])
        self.driver.send_keys(ele_info=ele_info["输入5G消息模板ID"],text="123456")
        self.driver.send_keys(ele_info=ele_info["输入5G短信签名"],text="爱邦保险")
        self.driver.click(ele_info=ele_info["添加5G参数"])
        self.driver.clicks(ele_info=ele_info["添加参数1"])
        self.driver.send_keys(ele_info=ele_info["参数1"],text="www.baidu.com")
        self.driver.click(ele_info=ele_info["提醒文案"])
        self.driver.send_keys(ele_info=ele_info["key1"], text=f"{Keys.CONTROL}, 'a'")
        time.sleep(1)
        self.driver.send_keys(ele_info=ele_info["key1"],text="parm1")
        self.driver.click(ele_info=ele_info["添加5G参数"])
        self.driver.click(ele_info=ele_info["添加参数2"])
        self.driver.send_keys(ele_info=ele_info["key2"], text=f"{Keys.CONTROL}, 'a'")
        time.sleep(1)
        self.driver.send_keys(ele_info=ele_info["key2"],text="parm2")
        time.sleep(1)
        self.driver.get_screenshot_as_file(rf"{get_project_path.project_path}\screenshot\{template_name}_5g.png")
        self.adjust_resolution(False)

    def adjust_resolution(self,default=True,times=5):
        if default:
            for i in range(times):
                pyautogui.hotkey('ctrl', '-')
                time.sleep(0.25)
        else :
            pyautogui.hotkey('ctrl', '0')

    def save_template(self):
        ele_info = self.page_eles["edit_sms_template"]
        self.driver.click(ele_info=ele_info["保存"])
        time.sleep(1)
        self.driver.click(ele_info=ele_info["确认"])
        time.sleep(2)

    def delete_template(self,group_name, template_name):
        self.enter_MouldChildList(group_name)
        time.sleep(1)
        self.query_template(template_name)
        ele_info = self.page_eles["template_list"]
        if self.driver.page_contains("暂无数据"):
            self.logger.info(f'未查询到数据，不执行删除')
            return
        template_num = self.driver.get_text(ele_info=ele_info["模板数量"])
        delete_times= extract_total_count(template_num)
        self.logger.info(f'查询到{delete_times}条数据，执行删除')
        self.driver.click(ele_info=ele_info["模板删除"])
        time.sleep(1)
        self.driver.click(ele_info=ele_info["取消删除"])
        self.driver.click(ele_info=ele_info["取消删除"])
        time.sleep(1)
        for i in range(delete_times):
            self.driver.click(ele_info=ele_info["模板删除"])
            time.sleep(1)
            self.driver.click(ele_info=ele_info["确认删除"])
            time.sleep(2)
        time.sleep(2)
        self.driver.get_screenshot_as_file(rf"{get_project_path.project_path}\screenshot\删除{template_name}.png")

    def edit_gzh_template(self, template_name):
        ele_info = self.page_eles["edit_gzh_template"]
        time.sleep(2)
        self.driver.click(ele_info=ele_info["选择公众号名称"])
        self.driver.click(ele_info=ele_info["选择粤西湛惠保"])
        time.sleep(1)
        self.driver.click(ele_info=ele_info["选择微信模板"])
        time.sleep(1)
        self.driver.action_keys(Keys.ENTER)
        url = "https://www.baidu.com?platformCode=abbxsc&channelCode=DX&channelSource=xf_dxt3zx&paymentChannel=xf_dxt3zx&leadsId={leadsId}&leadsSignature={leadsSignature}"
        self.driver.send_keys(ele_info=ele_info["跳转链接"], text=url)
        self.edit_url_parameter(template_name)
        self.adjust_resolution()
        self.driver.send_keyss(ele_info=ele_info["内容标签配置"], text="1")
        self.add_parameter()
        self.driver.get_screenshot_as_file(rf"{get_project_path.project_path}\screenshot\公众号{template_name}.png")
        self.adjust_resolution(False)

    def edit_mini_program_template(self, template_name):
        ele_info = self.page_eles["edit_mini_program_template"]
        time.sleep(2)
        self.driver.click(ele_info=ele_info["选择小程序主体"])
        self.driver.click(ele_info=ele_info["选择长得真好看"])
        time.sleep(1)
        self.driver.click(ele_info=ele_info["选择模板名称"])
        time.sleep(1)
        self.driver.click(ele_info=ele_info["选择模板"])
        self.adjust_resolution(times=4)
        url = "https://m.aibangbaoxian.net?linkType=upgrade&platformCode=abbxsc&channelCode=XCX&channelSource=bw_bdzttz_xcx&paymentChannel=bw_bdzttz_xcx"
        self.driver.send_keys(ele_info=ele_info["小程序路径"], text=url)
        self.edit_url_parameter(template_name)
        self.driver.click(ele_info=ele_info["路径加工"])
        self.driver.click(ele_info=ele_info["非强制授权"])
        time.sleep(1)
        self.driver.get_screenshot_as_file(rf"{get_project_path.project_path}\screenshot\非强制授权.png")
        time.sleep(1)
        self.driver.click(ele_info=ele_info["路径加工"])
        self.driver.click(ele_info=ele_info["强制授权"])
        self.driver.send_keyss(ele_info=ele_info["内容标签配置"], text="1")
        self.add_parameter()
        self.driver.get_screenshot_as_file(rf"{get_project_path.project_path}\screenshot\小程序{template_name}.png")
        self.adjust_resolution(False)